unit uSetingForm;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TSetingForm = class(TForm)
    Label1: TLabel;
    edtHost: TEdit;
    Label2: TLabel;
    edtPort: TEdit;
    Label3: TLabel;
    edtFtpDir: TEdit;
    Label4: TLabel;
    edtUpUser: TEdit;
    Label5: TLabel;
    edtUpPwd: TEdit;
    Label6: TLabel;
    edtDownUser: TEdit;
    Label7: TLabel;
    edtDownPwd: TEdit;
    Button1: TButton;
    Button2: TButton;
    procedure FormShow(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

implementation

uses
  uGlobal;

{$R *.dfm}

procedure TSetingForm.Button1Click(Sender: TObject);
  procedure PostParameterTable(const value, fieldName, paramName: string);
  var
    nRet: Integer;
  begin
    nRet := ExecuteQuery(Format('SELECT * FROM tbParameter WHERE ParamName=''%s''',
        [paramName]), G_ADOQuery);
    if nRet<0 then
      Exit;

    try
      if nRet=0 then
      begin
        G_ADOQuery.Append;
        G_ADOQuery.FieldByName(fieldName).Value := value;
        G_ADOQuery.FieldByName('ParamName').AsString := paramName;
        G_ADOQuery.Post;
      end else
      begin
        G_ADOQuery.Edit;
        G_ADOQuery.FieldByName(fieldName).Value := value;
        G_ADOQuery.Post;
      end;
    except
    end;
  end;
begin
  PostParameterTable(edtHost.Text, 'strValue', 'FtpHost');
  PostParameterTable(edtPort.Text, 'intValue', 'FtpPort');
  PostParameterTable(edtFtpDir.Text, 'strValue', 'FtpDir');
  PostParameterTable(edtUpUser.Text, 'strValue', 'UploadUser');
  PostParameterTable(edtUpPwd.Text, 'strValue', 'UploadPwd');
  PostParameterTable(edtDownUser.Text, 'strValue', 'DownloadUser');
  PostParameterTable(edtDownPwd.Text, 'strValue', 'DownloadPwd');
  Close;
end;

procedure TSetingForm.Button2Click(Sender: TObject);
begin
  Close;
end;

procedure TSetingForm.FormShow(Sender: TObject);
var
  nRet: Integer;
begin
  nRet := ExecuteQuery(Format('SELECT strValue FROM tbParameter WHERE ParamName=''FtpHost''', []), G_ADOQuery);
  if nRet<0 then
  begin
    Close;
    Exit;
  end;
  if nRet>0 then
    edtHost.Text := G_ADOQuery.Fields[0].AsString;

  nRet := ExecuteQuery(Format('SELECT intValue FROM tbParameter WHERE ParamName=''FtpPort''', []), G_ADOQuery);
  if nRet<0 then
  begin
    Close;
    Exit;
  end;
  if nRet>0 then
    edtPort.Text := IntToStr(G_ADOQuery.Fields[0].AsInteger);

  nRet := ExecuteQuery(Format('SELECT strValue FROM tbParameter WHERE ParamName=''FtpDir''', []), G_ADOQuery);
  if nRet<0 then
  begin
    Close;
    Exit;
  end;
  if nRet>0 then
    edtFtpDir.Text := G_ADOQuery.Fields[0].AsString;

  nRet := ExecuteQuery(Format('SELECT strValue FROM tbParameter WHERE ParamName=''UploadUser''', []), G_ADOQuery);
  if nRet<0 then
  begin
    Close;
    Exit;
  end;
  if nRet>0 then
    edtUpUser.Text := G_ADOQuery.Fields[0].AsString;

  nRet := ExecuteQuery(Format('SELECT strValue FROM tbParameter WHERE ParamName=''UploadPwd''', []), G_ADOQuery);
  if nRet<0 then
  begin
    Close;
    Exit;
  end;
  if nRet>0 then
    edtUpPwd.Text := G_ADOQuery.Fields[0].AsString;

  nRet := ExecuteQuery(Format('SELECT strValue FROM tbParameter WHERE ParamName=''DownloadUser''', []), G_ADOQuery);
  if nRet<0 then
  begin
    Close;
    Exit;
  end;
  if nRet>0 then
    edtDownUser.Text := G_ADOQuery.Fields[0].AsString;

  nRet := ExecuteQuery(Format('SELECT strValue FROM tbParameter WHERE ParamName=''DownloadPwd''', []), G_ADOQuery);
  if nRet<0 then
  begin
    Close;
    Exit;
  end;
  if nRet>0 then
    edtDownPwd.Text := G_ADOQuery.Fields[0].AsString;
end;

end.
